package com.imooc.pan.server.modules.user.controller;

import com.imooc.pan.core.response.R;
import com.imooc.pan.core.utils.IdUtil;
import com.imooc.pan.server.common.annotation.LoginIgnore;
import com.imooc.pan.server.common.utils.UserIdUtil;
import com.imooc.pan.server.modules.user.context.*;
import com.imooc.pan.server.modules.user.converter.UserConverter;
import com.imooc.pan.server.modules.user.po.*;
import com.imooc.pan.server.modules.user.service.RPanUserService;
import com.imooc.pan.server.modules.user.vo.UserInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private RPanUserService userService;
    @Autowired
    private UserConverter userConverter;
    @LoginIgnore
    @PostMapping("/register")
    public R registerUser(@Validated @RequestBody UserRegisterPo userRegisterPo) {
        Long userId =  userService.registerUser(userConverter.userRegisterPo2UserRegisterContext(userRegisterPo));
        return R.success(IdUtil.encrypt(userId));
    }
    @LoginIgnore
    @PostMapping("/login")
    public R login(@Validated @RequestBody UserLoginPO userLoginPO) {
        UserLoginContext userLoginContext = userConverter.userLoginPO2UserLoginContext(userLoginPO);
        String accessToken = userService.login(userLoginContext);
        return R.data(accessToken);
    }
    @PostMapping("/exit")
    public R exit() {
        userService.exit(UserIdUtil.get());
        return R.success();
    }
    @LoginIgnore
    @PostMapping("/username/check")
    public R checkUsername(@Validated @RequestBody CheckUsernamePO checkUsernamePO) {
        CheckUsernameContext checkUsernameContext = userConverter.checkUsernamePO2CheckUsernameContext(checkUsernamePO);
        String question = userService.checkUsername(checkUsernameContext);
        return R.data(question);
    }
    @LoginIgnore
    @PostMapping("/answer/check")
    public R checkAnswer(@Validated @RequestBody CheckAnswerPO checkAnswerPO) {
        CheckAnswerContext checkAnswerContext = userConverter.checkAnswerPO2CheckAnswerContext(checkAnswerPO);
        String token = userService.checkAnswer(checkAnswerContext);
        return R.data(token);
    }

    /**
     * 用户重置密码
     * @param resetPasswordPO
     * @return
     */
    @PostMapping("/password/reset")
    @LoginIgnore
    public R resetPassword(@Validated @RequestBody ResetPasswordPO resetPasswordPO) {
        ResetPasswordContext resetPasswordContext = userConverter.resetPasswordPO2ResetPasswordContext(resetPasswordPO);
        userService.resetPassword(resetPasswordContext);
        return R.success();
    }

    /**
     * 用户在线修改密码
     * @param changePasswordPO
     * @return
     */
    @PostMapping("/password/change")
    public R changePassword(@Validated @RequestBody ChangePasswordPO changePasswordPO) {
        ChangePasswordContext changePasswordContext = userConverter.changePasswordPO2ChangePasswordContext(changePasswordPO);
        changePasswordContext.setUserId(UserIdUtil.get());
        userService.changePassword(changePasswordContext);
        return R.success();
    }

    @GetMapping("/")
    public R<UserInfoVO> info() {
        UserInfoVO userInfoVO = userService.info(UserIdUtil.get());
        return R.data(userInfoVO);
    }
}
